package com.llxwz.controller;


import com.llxwz.entity.DiscussPost;
import com.llxwz.entity.Page;
import com.llxwz.entity.Tag;
import com.llxwz.service.TagService;
import com.llxwz.util.CommunityConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.*;

@Controller
public class TagController implements CommunityConstant {

    @Autowired
    private TagService tagService;


    @GetMapping("/getTagList/{tagName}/{entityType}")
    public String getTagList(@PathVariable("tagName")String tagName,
                              @PathVariable("entityType")int entityType,
                              Model model, Page page){

        page.setRows(tagService.getTagRows(tagName,entityType));
        page.setPath("/getTagList/"+tagName+"/"+entityType);

        List<Tag> tags = tagService.getTagList(tagName,entityType,page.getCurrent(), page.getLimit());

        if(tags!=null){
            model.addAttribute("tags",tags);
        }

        return "site/admin/tagList";
    }

    @PostMapping("/setTag")
    public String setTag(int id,String tagName,int entityType){
        tagService.updateTagName(id,tagName);
        tagService.updateEntityType(id,entityType);
        return "redirect:/getTagList/null/-1";
    }

    @PostMapping("/banTag")
    public String banTag(int id){
        tagService.banTag(id);
        return "redirect:/getTagList/null/-1";
    }

    @PostMapping("/unbanTag")
    public String unbanTag(int id){
        tagService.unbanTag(id);
        return "redirect:/getTagList/null/-1";
    }

    @PostMapping("/addTag")
    public String addTag(String tagName,int entityType){
        Tag tag = new Tag();
        tag.setTagName(tagName);
        tag.setStatus(0);
        tag.setEntityType(entityType);
        tag.setCreateTime(new Date());
        tagService.addTag(tag);
        return "redirect:/getTagList/null/-1";
    }

}
